package com.appengine.utils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import com.datatypes.AEuser;
import com.datatypes.Activity;
import com.datatypes.ActivityTracks;
import com.datatypes.Friend;
import com.datatypes.FriendsZone;
import com.datatypes.OfflineLogs;
import com.datatypes.Settings;
import com.datatypes.Track;
import com.datatypes.User;
import com.datatypes.UserTracks;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonUtils {
	public static Gson gson = new Gson();

	public static AEuser ParseUserWithGson(String facebookid) {
		AEuser user = null;
		String jsonS = null;
		JSONObject jsonO = null;

		try {
			jsonO = JsonUtils
					.readJsonFromUrl("http://sporteamws.appspot.com/JSONcomm?facebookid="
							+ facebookid);
			jsonS = (String) jsonO.get("userjson");
			user = gson.fromJson(jsonS, AEuser.class);
		} catch (IOException e) {
			Log.e("GsonUtils", e.toString());
		} catch (JSONException e) {
			Log.e("GsonUtils", e.toString());
		}

		return user;
	}

	public static String ConvertUserToGsonString(User user) {
		return gson.toJson(user);
	}

	public static User ConvertGsonStringToUser(String UserStr) {
		return gson.fromJson(UserStr, User.class);
	}

	public static String ConvertAEuserToGsonString(AEuser ae_user) {
		return gson.toJson(ae_user);
	}

	public static AEuser ConvertGsonStringToAEuser(String AEUserStr) {
		return gson.fromJson(AEUserStr, AEuser.class);
	}

	public static String ConvertTrackToGsonString(Track track) {
		return gson.toJson(track);
	}

	public static Track ConvertGsonStringToTrack(String TrackStr) {
		return gson.fromJson(TrackStr, Track.class);
	}

	public static Activity ConvertGsonStringToActivity(String ActStr) {
		return gson.fromJson(ActStr, Activity.class);
	}

	public static String ConvertActivityToGsonString(Activity act) {
		return gson.toJson(act);
	}

	public static ActivityTracks ConvertGsonStringToActivityTracks(String ActStr) {
		return gson.fromJson(ActStr, ActivityTracks.class);
	}

	public static String ConvertActivityTracksToGsonString(ActivityTracks act) {
		return gson.toJson(act);
	}

	public static UserTracks ConvertGsonStringToUserTracks(String userTracksStr) {
		return gson.fromJson(userTracksStr, UserTracks.class);
	}

	public static String ConvertUserTracksToGsonString(UserTracks act) {
		return gson.toJson(act);
	}

	public static FriendsZone ConvertGsonStringToFriendsZone(String fzStr) {
		return gson.fromJson(fzStr, FriendsZone.class);
	}

	public static String ConvertFriendsZoneToGsonString(FriendsZone fz) {
		return gson.toJson(fz);
	}

	public static String ConvertTracksToGsonString(ArrayList<Track> tracks) {
		return gson.toJson(tracks);
	}

	public static ArrayList<Track> ConvertGsonStringToTracks(String tracksStr) {
		Type collectionType = new TypeToken<ArrayList<Track>>() {
		}.getType();
		return gson.fromJson(tracksStr, collectionType);
	}

	public static String ConvertActivitiesToGsonString(
			ArrayList<Activity> activities) {
		return gson.toJson(activities);
	}

	public static ArrayList<Activity> ConvertGsonStringToActivities(
			String actsStr) {
		Type collectionType = new TypeToken<ArrayList<Activity>>() {
		}.getType();
		return gson.fromJson(actsStr, collectionType);
	}

	public static ArrayList<String> ConvertGsonStringToFriends(String friendsStr) {
		Type collectionType = new TypeToken<ArrayList<String>>() {
		}.getType();
		return gson.fromJson(friendsStr, collectionType);
	}

	public static String ConvertFriendsToGsonString(ArrayList<String> friends) {
		return gson.toJson(friends);
	}

	public static Friend ConvertGsonStringToFriend(String friendStr) {
		return gson.fromJson(friendStr, Friend.class);
	}

	public static String ConvertFriendToGsonString(Friend friend) {
		return gson.toJson(friend);
	}

	public static OfflineLogs ConvertGsonStringToOfflineLogs(String oll) {
		return gson.fromJson(oll, OfflineLogs.class);
	}

	public static String ConvertOfflineLogsToGsonString(OfflineLogs oll) {
		return gson.toJson(oll);
	}

	public static String ConvertSettingsToGsonString(Settings s) {
		return gson.toJson(s);
	}

	public static Settings ConvertGsonStringToSettings(String settings) {
		return gson.fromJson(settings, Settings.class);
	}

}
